﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccess.ProxyConnections;
using DataAccess.SingleSignOn;
using Single_SignOn.Areas.Web.Models;
using Single_SignOn.Attributes;
using Single_SignOn.Json;
using Single_SignOn.Models;

namespace Single_SignOn.Areas.Web.Controllers
{
    
    public class ApplicationListController : Controller
    {
        public AuthenConnection oAuth;

        public ApplicationListModels _model;
        //
        // GET: /PC/ApplicationList/
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            oAuth = new AuthenConnection();

            ApplicationListModels model = GetListModels();

            ViewBag.CompanyName = model.Companies[0].Name;
            
            ViewBag.ByteImages = (Byte[])(oAuth.getCompanyLogo(model.Companies[0].Name));

            return View(model);
        }
        

        public ActionResult ChangeCompany(string company)
        {
            
            oAuth = new AuthenConnection();

            ApplicationListModels model = GetListModels(company);

            ViewBag.CompanyName = company;
        
            ViewBag.ByteImages = (Byte[])(oAuth.getCompanyLogo(company));

            return View("Index",model);
        }

        public ActionResult ClickApplicationList(string applno, string company, string language)
        {

            oAuth = new AuthenConnection();

            String token = Guid.NewGuid().ToString();

            UserProfile o = (UserProfile)Session["UserInfo"];

            String group = oAuth.getGroup(o.Username, applno,
                company);

            DataTable dt = oAuth.getApplicationListByApplNo(o.Username, company,
                applno);

            string url = (String)dt.Rows[0]["url"];
            string result = oAuth.insertTokenApp(o.Username, o.Password, token, applno,
                (String)dt.Rows[0]["applname"]
                , group, o.Division, language, company);

            return Redirect(string.Format("{0}?token={1}"
                    , url
                    ,result));
        }

        public ActionResult GetToken(string applno, string company, string language)
        {

            oAuth = new AuthenConnection();

            String token = Guid.NewGuid().ToString();

            UserProfile o = (UserProfile)Session["UserInfo"];

            String group = oAuth.getGroup(o.Username, applno,
                company);

            DataTable dt = oAuth.getApplicationListByApplNo(o.Username, company,
                applno);

            string url = (String)dt.Rows[0]["url"];
            string result = oAuth.insertTokenApp(o.Username, o.Password, token, applno,
                (String)dt.Rows[0]["applname"]
                , group, o.Division, language, company);

            // Updated Current Language CurrentUI
            oAuth.upUserCurrentUI(o.Username, language, company);

            return Redirect(string.Format("{0}?token={1}"
                    , url
                    , result));
        }

       
        public ActionResult ChangeLanguage(String language, String company)
        {
            /**/
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(language); ;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar
                = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;
            
            
            oAuth = new AuthenConnection();

            ApplicationListModels model = GetListModels(company);

            ViewBag.CompanyName = company;

            ViewBag.ByteImages = (Byte[])(oAuth.getCompanyLogo(company));

            ((UserProfile)Session["UserInfo"]).Language = language;

            return View("Index", model);
        }

        protected ApplicationListModels GetListModels(String comp = null)
        {
            _model = new ApplicationListModels();
            {
                oAuth = new AuthenConnection();

                // get Company
                DataTable dtCompany = oAuth.getCompany((string)((UserProfile)Session["UserInfo"]).Username);
                Company company;
                if (dtCompany.Rows.Count > 0)
                {
                    List<Company> Companies = new List<Company>();
                    for (int i = 0; i < dtCompany.Rows.Count; i++)
                    {
                        company = new Company();
                        company.Name = (string)dtCompany.Rows[i]["company_code"];
                        company.Description = (string)dtCompany.Rows[i]["company_name_en"];
                        Companies.Add(company);
                    }
                    _model.Companies = Companies;
                }

                // get Language
                DataTable dtLanguage = oAuth.getLanguage();
                Language language;
                if (dtLanguage.Rows.Count > 0)
                {
                    List<Language> languages = new List<Language>();
                    for (int i = 0; i < dtLanguage.Rows.Count; i++)
                    {
                        language = new Language();
                        language.Name = (string)dtLanguage.Rows[i]["lang_culture_name"];
                        language.Description = (string)dtLanguage.Rows[i]["lang_name"];
                        languages.Add(language);
                    }
                    _model.Languages = languages;
                }

                //get Application List
                DataTable dtApplication = 
                    oAuth.getApplicationList((string)((UserProfile)Session["UserInfo"]).Username, 
                    (String.IsNullOrEmpty(comp)
                    ? _model.Companies[0].Name
                    :comp));

                ApplicationList application;
                if (dtApplication.Rows.Count > 0)
                {
                    List<ApplicationList> applicationLists = new List<ApplicationList>();
                    for (int i = 0; i < dtApplication.Rows.Count; i++)
                    {
                        application = new ApplicationList();
                        application.No = i + 1; 
                        application.ApplNo = (string)dtApplication.Rows[i]["applno"];
                        application.Application = (string)dtApplication.Rows[i]["applname"];
                        application.Description = (string)dtApplication.Rows[i]["description"];
                        application.Url = (string)dtApplication.Rows[i]["url"];
                        applicationLists.Add(application);
                    }
                    _model.ApplicationLists = applicationLists;
                }
            }
            return _model;
        }

        /*
        public JsonResult ChangeLanguage(String language, String company)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(language); ;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar
                = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;
            
            oAuth = new AuthenConnection();

            ApplicationListModels model = GetListModels(company);

            ViewBag.ComanyName = model.Companies[0].Name;

            ViewBag.ByteImages = (Byte[])(oAuth.getCompanyLogo(model.Companies[0].Name));
           
            ((UserProfile) Session["UserInfo"]).Language = language;
            
            var result = new object();
            result = new
            {
                language = language,
                company = company
            };
            return Json(result, JsonRequestBehavior.AllowGet);
            
        }
*/
    }
}
